import java.io.*;
import java.util.*;
import org.xml.sax.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class PronomXMLContentHandler implements ContentHandler {
	
	private PrintWriter out;
	private BufferedWriter log_writer;
	private int valid = 1;
	String soap_request = "";
	private Vector required = new Vector();
	private Hashtable exists = new Hashtable();
	private Hashtable parent = new Hashtable();
	private Hashtable openNodes = new Hashtable();
	private String currentNode;
	private String value;
		
	PronomXMLContentHandler(PrintWriter client, BufferedWriter inlog_writer, String insoap_request) {
		out = client;
		log_writer = inlog_writer;
		soap_request = insoap_request;
		System.out.println("SOAP! : " + insoap_request);
		if (insoap_request.equalsIgnoreCase("http://pp.pronom.nationalarchives.gov.uk/getFormatPropertyRiskIn")) {
			required.add("http://schemas.xmlsoap.org/soap/envelope/#Envelope");
			required.add("http://schemas.xmlsoap.org/soap/envelope/#Body");
			parent.put("http://schemas.xmlsoap.org/soap/envelope/#Body","http://schemas.xmlsoap.org/soap/envelope/#Envelope");
			required.add("http://pp.pronom.nationalarchives.gov.uk#FormatPropertyPair");
			parent.put("http://pp.pronom.nationalarchives.gov.uk#FormatPropertyPair","http://schemas.xmlsoap.org/soap/envelope/#Body");
			required.add("http://pp.pronom.nationalarchives.gov.uk#propertyID");
			parent.put("http://pp.pronom.nationalarchives.gov.uk#propertyId","http://pp.pronom.nationalarchives.gov.uk#FormatPropertyPair");
			required.add("http://pp.pronom.nationalarchives.gov.uk#PUID");
			parent.put("http://pp.pronom.nationalarchives.gov.uk#PUID","http://pp.pronom.nationalarchives.gov.uk#FormatPropertyPair");
		}
		if (insoap_request.equalsIgnoreCase("http://pp.pronom.nationalarchives.gov.uk/getFormatRiskIn")) {
			required.add("http://schemas.xmlsoap.org/soap/envelope/#Envelope");
			required.add("http://schemas.xmlsoap.org/soap/envelope/#Body");
			parent.put("http://schemas.xmlsoap.org/soap/envelope/#Body","http://schemas.xmlsoap.org/soap/envelope/#Envelope");
			required.add("http://pp.pronom.nationalarchives.gov.uk#PUID");
			parent.put("http://pp.pronom.nationalarchives.gov.uk#PUID","http://schemas.xmlsoap.org/soap/envelope/#Body");
			required.add("http://pp.pronom.nationalarchives.gov.uk#Value");
			parent.put("http://pp.pronom.nationalarchives.gov.uk#Value","http://pp.pronom.nationalarchives.gov.uk#PUID");
			
		}
	}

	public void setDocumentLocator (Locator locator) { 
	}

	public void startDocument () throws SAXException  {
	}

	public void endDocument() throws SAXException  { 
		System.out.println("Vector size: " + required.size());
		Enumeration e = required.elements();
		String content = "";
		while (e.hasMoreElements()) {
			String check = e.nextElement().toString();
			System.out.println("Checking: " + check);
			System.out.println("Exisits? : " + (Integer)exists.get(check));
			if ((Integer)exists.get(check) == null) {
				valid = 0;
				content = "<div align=\"center\"><br/><br/><b>Invalid Document Supplied</b><br/><br/>The the node <" + check + "> didn't exist in the document or was in the wrong place.</div>"; 
				break;
			}
		}
		System.out.println("valid: " + valid);
		System.out.println("value: " + value);
		if (valid == 0) {
			out.println("HTTP/1.1 400 Bad Request");
			out.println("Date: " + getDateTime());
			out.println("Server: Service Controller");
			out.println("X-Powered-By: Java");
			out.println("Connection: close");
			out.println("Content-Type: text/html; charset=utf-8");
			int ccount = content.toCharArray().length;
			out.println("Content-length: " + ccount);
			out.println("");
			out.println(content);
		} else {
			if (soap_request.equalsIgnoreCase("http://pp.pronom.nationalarchives.gov.uk/getFormatPropertyRiskIn")) {
				content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\t<soap:Body>\n\t\t<FormatPropertyRisk xmlns=\"http://pp.pronom.nationalarchives.gov.uk\">\n\t\t\t<risk>200.20</risk>\n\t\t</FormatPropertyRisk>\n\t</soap:Body>\n</soap:Envelope>"; 
			}
			if (soap_request.equalsIgnoreCase("http://pp.pronom.nationalarchives.gov.uk/getFormatRiskIn")) {
				String risk = "";
				if (value.equals("fmt/18")) {
					risk = "2000";
				} else if (value.equals("fmt/126")) {
					risk = "1466.22";
				} else if (value.equals("fmt/111")) {
					risk = "183.86";
				} else {
					risk = "2001";
				}
				content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n\t<soap:Body>\n\t\t<FormatRisk xmlns=\"http://pp.pronom.nationalarchives.gov.uk\">\n\t\t\t<risk>"+risk+"</risk>\n\t\t</FormatRisk>\n\t</soap:Body>\n</soap:Envelope>"; 
			}
			int ccount = content.toCharArray().length;
			out.println("HTTP/1.1 200 OK");
			out.println("Date: " + getDateTime());
			out.println("Server: Service Controller");
			out.println("X-Powered-By: Java");
			out.println("Connection: close");
			out.println("Content-Type: text/xml; charset=utf-8");
			out.println("Content-length: " + ccount);
			out.println("");
			out.println(content);
		}
	}

	public void startPrefixMapping (String prefix, String uri) throws SAXException  {
	}

	public void endPrefixMapping (String prefix) throws SAXException  { 
	}

	public void startElement (String uri, String localName, String qName, Attributes atts) throws SAXException  {
		String uriLocalName = uri + "#" + localName;
		currentNode = uriLocalName;
		System.out.println("Node : " + uriLocalName);
		exists.put(uriLocalName,1);
		String node_parent = "";
		try {
			node_parent = (String)parent.get(uriLocalName);
		} catch (Exception e) {
			//e.printStackTrace();
			node_parent = "";
		}
		if (node_parent != null) {
			if ((Integer)openNodes.get(node_parent) == 1) {
				openNodes.put(uriLocalName,1);
			} else {
				valid = 0;
			}
		} else {
			openNodes.put(uriLocalName,1);
		}
	}

	public void endElement (String uri, String localName, String qName) throws SAXException { 
		String uriLocalName = uri + "#" + localName;
		openNodes.put(uriLocalName,0);
	}

	public void characters (char ch[], int start, int length) throws SAXException { 
		String handle = "";
		if (currentNode.equals("http://pp.pronom.nationalarchives.gov.uk#Value")) {
			for (int i=start;i<(start+length);i++) {
				handle = handle + ch[i];
			}
			value = handle;
		}
	}

	public void ignorableWhitespace (char ch[], int start, int length) throws SAXException { 
	}

	public void processingInstruction (String target, String data) throws SAXException { 
	}

	public void skippedEntity (String name) throws SAXException { 
	}
	
	private String getDateTime() {
		DateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");
		Date date = new Date();
		return dateFormat.format(date);
	}
}

